#include "texteditor.h"
#include "ui_texteditor.h"

#include<QFile>
#include<QMessageBox>
#include<QTextStream>
#include<QFileInfo>
#include<QFileDialog>
#include<QTextStream>
#include<QToolBar>
TextEditor::TextEditor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TextEditor)
{
    ui->setupUi(this);
//    ui->mainToolBar = new QToolBar(this);
    ui->mainToolBar->addAction(ui->action_New);
    ui->mainToolBar->addAction(ui->action_Open);
    ui->mainToolBar->addAction(ui->action_Save);
    this->setWindowTitle(tr("Text Editor"));
    //this->setWindowIcon(":/images/images/windowTitle.png");

}

TextEditor::~TextEditor()
{
    delete ui;
}

bool TextEditor::saveFile(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly | QFile::Text))
    {
        QMessageBox::warning(this,tr("save file"),tr("can't save file %1:\n %2").arg(fileName)
                             .arg(file.errorString()));
        return false;
    }
    QMessageBox msgBox;
    msgBox.setWindowTitle(tr("Warning: "));
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setText(this->curFile+tr(" \n save or not?"));
    //msgBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
    if(msgBox.exec() == /*QMessageBox::Yes*/QMessageBox::Save)
    {
        QTextStream out(&file);
        out<<ui->textEdit->toPlainText();
        this->isSaved = true;
        this->curFile = QFileInfo(fileName).canonicalFilePath();
        this->setWindowTitle(curFile);
        return true;
    }
    return false;
}

void TextEditor::saveFileAs()
{
    QString fileName = QFileDialog::getSaveFileName(this,tr("save as"),curFile);
    if(!fileName.isEmpty()) //if the file name is not empty
    {
        this->saveFile(fileName);
    }

}

void TextEditor::on_action_New_triggered()
{
    this->curFile = tr("untitled.txt");
    this->setWindowTitle(curFile);
    ui->textEdit->clear();
    ui->textEdit->setVisible(true);
}
void TextEditor::saveFielOrNot()
{
    if(ui->textEdit->document()->isModified())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle(tr("Warning:"));
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(this->curFile+tr(" save or not?"));
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        if(msgBox.exec() == QMessageBox::Yes)
           if(this->isSaved == true)
                this->saveFile(this->curFile);
            //else     need to add something

    }
}

bool TextEditor::loadFile(const QString &fileName)
{
    QFile file(fileName);
    if(! file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this,tr("Warning:"),tr("can not read file %1,%2").arg(fileName)
                                                    .arg(file.errorString()));
        return false;
    }
     QTextStream in(&file);
     ui->textEdit->setText(in.readAll());
     this->curFile = QFileInfo(fileName).canonicalFilePath();
     this->setWindowTitle(curFile);
     return true;
}
void TextEditor::on_action_Open_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this);
    if(!filename.isEmpty())  //if filename is not empty
    {
        this->loadFile(filename);
    }
    ui->textEdit->setVisible(true);
}



void TextEditor::on_action_Save_triggered()
{
    if(this->isSaved)
        this->saveFile(this->curFile);
    else
        this->saveFileAs();
}

void TextEditor::on_action_SaveAs_triggered()
{
     this->saveFileAs();
}

void TextEditor::on_action_Quit_triggered()
{
    qApp->quit();
}

void TextEditor::on_actionAbout_triggered()
{
    QMessageBox::about(NULL,tr("About Text Editor"),
                       tr("<h2>Text Editor version 0.01</h2>"
                          "<p><h3>Copyright &copy;2010 software Inc.</h3></p>"
                          "<p>Text Editor is a samall application which is writed to study qt."
                          "<p>Author:David Luo E-mail:luodayuan2@gmail.com"));
}

void TextEditor::on_actionAboutQt_triggered()
{
    QMessageBox::aboutQt(NULL,tr("Qt Application"));
}
